package com.wzgl.core.model;



import java.util.List;

import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;
import com.wzgl.core.base.Table;
import com.wzgl.core.baseModel.BaseMagic;
import com.wzgl.core.utils.StringUtils;

/***
 * 爬虫主表
 * @author Administrator
 *
 */
@Table(tableName = "Tmagic", primaryKey = "fid")
public class Tmagic extends BaseMagic<Tmagic> {
	private Log log=Log.getLog(Tmagic.class);
	private static final long serialVersionUID = -1L;
	public static final Tmagic dao = new Tmagic();
	
	public List<Tmagic> getAll(){
		return dao.find("select * from Tmagic");
	}
	
	public Tmagic findById(final String fid) {
		//后期使用缓存,前期不考虑
//		return dao.getCache(fid, new IDataLoader() {
//			@Override
//			public Object load() {
//				return dao.doFindFirst("fid = ?", fid);
//			}
//		});
		return dao.doFindFirst("fid = ?", fid);
	}
	public Tmagic findByUrl(final String furl) {
		//后期使用缓存,前期不考虑
//		return dao.getCache(fid, new IDataLoader() {
//			@Override
//			public Object load() {
//				return dao.doFindFirst("fid = ?", fid);
//			}
//		});
		return dao.doFindFirst("furl = ?", furl);
	}
	public Page<Tmagic> paginate(int pageNumber, int pageSize,String fpage) {
		if(StringUtils.isNotBlank(fpage)){
			return paginate(pageNumber, pageSize, "select *", "from Tmagic where fpage = ? ",fpage);
		}
		return paginate(pageNumber, pageSize, "select *", "from Tmagic");
	}
	
	public Tmagic findByEmail(final String fpage){
		return dao.getCache(fpage, new IDataLoader() {
			@Override
			public Object load() {
				return dao.doFindFirst("fpage = ?", fpage);
			}
		});
	}
	
	public Tmagic findInSession(String fid) {
		// 传入的userId为空直接返回null
		if (StrKit.isBlank(fid)) {
			return null;
		}
		return loadInSession(fid);
	}

	public Tmagic loadInSession(final String fid) {
		return CacheKit.get(CACHE_NAME, fid, new IDataLoader() {
			@Override
			public Object load() {
				return findById(fid);
			}
		});
	}
}
